package com.iot08.admin.service;

import com.iot08.common.entity.admin.Agent;
import com.iot08.common.entity.admin.Channel;
import com.iot08.common.service.base.BaseAdminService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 旧版的厂商 信息对象
 *
 * @Author: Weichao
 * @Date: 2024/08/02/13:43
 * @Description:
 */
@Slf4j
@Service
public class AgentService extends BaseAdminService<Agent> {
    @Resource
    private ChannelService channelService;

    /***
     * @Description: 查找未被替换的 Agent ,全部转换成 Channel,存储起来.
     * @Author: Weichao
     * @Date: 2024/8/2
     */
    public List<Channel> doChangeChannelList() {
        //找 changeCCTime 不存在, 或者 changeCCTime < modifytime  的 Agent
//        Criteria criteria = Criteria.where("changeCCTime").exists(false);
//        criteria.orOperator(Criteria.where("changeCCTime").lt(Criteria.where("modifytime")));
//        List<Agent> agentList = find(new Query().addCriteria(criteria));
        List<Agent> agentList = find(new Query());
        List<Channel> channelList = new ArrayList<>();
        for (Agent itemA : agentList) {
            if(itemA.getModifytime() != null
                    && itemA.getChangeCCTime() != null
                    && itemA.getModifytime().getTime() <= itemA.getChangeCCTime().getTime()){
                //跳过
                continue;
            }
            if(channelService.count(new Query(Criteria.where("_id").is(""+itemA.getAgent_code()))) == 0){
                Channel channel = new Channel();
                channel.setName(itemA.getUsername());
                channel.setStatus(itemA.getStatus());
                channel.setAccount(itemA.getAccount());
                channel.setPassword(itemA.getPassword());
                channel.setChannelCode(itemA.getAgent_code());
                //TODO 这里需要兼容一下, 使用 agent_code 当 channel 的ID
                channel.setId(itemA.getAgent_code().toString());
                channel.setImportNum(itemA.getImport_num());
                channel.setActiveNum(itemA.getActive_num());
                channel.setShiftLimit(0);
                channel.setUnbundling(0);
                channel.setCanCheckPosition(0);
                channel.setCheckDActinoStatus(0);
                channel.setCreator(itemA.getCreater());
                channel.setLasttime(itemA.getLasttime());
                channel.setUpdateTime(itemA.getLasttime());
                channel.setCreateTime(itemA.getMaketime());
                channel.setDeleted(0);
                channelService.save(channel);
                log.info("未被替换的 Agent ,全部转换成 Channel,存储起来.厂商名:{} channelCode:{}",itemA.getUsername(), itemA.getAgent_code());
                channelList.add(channel);
                // 旧记录 记录 兼容时间
                itemA.setChangeCCTime(new Date());
                save(itemA);
            }
        }
        return channelList;
    }

}
